import { defineStore } from 'pinia'

export interface RootState {
  FDZCInfo: Record<string, any>
  JDWDInfo: any
  userFDInfo: any
  userWXInfo: any
  user: any
  token: any
  loading: boolean
  enterprise: any
  audioPlayingState: string
  innerAudioContext: any
  audioFileUrl: string
  isSameAudioFileUrl: boolean
  riskObj: Record<string, any>
  rectificationObj: Record<string, any>
  leasedFactoryInfo: any
  CODE_FXDJ: any[]
  CODE_HCTJ: any[]
  CODE_SGLXS: any[]
  JCYD: any[]
  CODE_WX: any
  FZRQM_Obj: any
  ZC_ZT: Record<string, any>
}

export const useAppStore = defineStore('app', {
  state: (): RootState => ({
    FDZCInfo: {},
    JDWDInfo: null,
    userFDInfo: null,
    userWXInfo: null,
    user: null,
    token: null,
    loading: false,
    enterprise: null,
    audioPlayingState: 'stop',
    innerAudioContext: null,
    audioFileUrl: '',
    isSameAudioFileUrl: false,
    riskObj: {},
    rectificationObj: {},
    leasedFactoryInfo: null,
    CODE_FXDJ: [],
    CODE_HCTJ: [],
    CODE_SGLXS: [],
    JCYD: [],
    CODE_WX: null,
    FZRQM_Obj: null,
    ZC_ZT: {}
  }),

  getters: {
    isAuthenticated: (state) => !!state.token,
    currentUser: (state) => state.user,
    isLoading: (state) => state.loading
  },

  actions: {
    setUserToken(token: any) {
      this.token = token
    },

    setUser(user: any) {
      this.user = user
    },

    setLoading(loading: boolean) {
      this.loading = loading
    },

    setWechatUserInfo(userInfo: any) {
      this.userWXInfo = userInfo
    },

    clearAuth() {
      this.token = null
      this.user = null
      this.userWXInfo = null
    }
  }
})
